package com.glorysoft.hlgdpda.ui.materialandtool.view

import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseActivity
import com.glorysoft.hlgdpda.databinding.ActivityBatchReplaceCarrierBinding
import com.glorysoft.hlgdpda.entity.LotEntity
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.ui.materialandtool.viewmodel.BatchUnBindCarrierViewModel
import kotlinx.android.synthetic.main.activity_batch_replace_carrier.*

/**
 * FileName: BatchUnBindCarrierActivity
 * Author: XJH
 * Date: 2021/12/14 11:28
 * Description:
 * History:
 */
class BatchReplaceCarrierActivity : BaseActivity<BatchUnBindCarrierViewModel,ActivityBatchReplaceCarrierBinding>() {
    var mLayoutViews = arrayListOf<LinearLayout>()
    var mTvLotViews = arrayListOf<TextView>()
    var mTvOldCarrierViews = arrayListOf<TextView>()
    var mEtNewCarrierViews = arrayListOf<EditText>()
    override fun layoutId(): Int {
        return R.layout.activity_batch_replace_carrier
    }

    override fun initVariableId(): Int {
        return BR.batchUnbindViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("批量更换载具")
        setRightListener("更换") forEach@{
            viewModel.newCarrierList.clear()
            mEtNewCarrierViews.forEachIndexed{index, editText ->
                if (editText.text.toString().isEmpty() && mLayoutViews[index].visibility == View.VISIBLE){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("列表中有载具未输入",false)
                    return@forEach
                }else{
                    viewModel.newCarrierList.add(editText.text.toString().trim())
                }
            }
            viewModel.replace()
        }

        mLayoutViews = arrayListOf(llContent1,llContent2,llContent3,llContent4,llContent5,llContent6,llContent7,llContent8)
        mTvLotViews = arrayListOf(tvLot1,tvLot2,tvLot3,tvLot4,tvLot5,tvLot6,tvLot7,tvLot8)
        mTvOldCarrierViews = arrayListOf(tvOldCarrier1,tvOldCarrier2,tvOldCarrier3,tvOldCarrier4,tvOldCarrier5,tvOldCarrier6,tvOldCarrier7,tvOldCarrier8)
        mEtNewCarrierViews = arrayListOf(etNewCarrier1,etNewCarrier2,etNewCarrier3,etNewCarrier4,etNewCarrier5,etNewCarrier6,etNewCarrier7,etNewCarrier8)
        initEdit(etCarrierID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.searchCarrierInfo(s)
            }

        })

        initEdit(etLot,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.searchLotInfo(s)
            }

        })

        mEtNewCarrierViews.forEachIndexed{index, editText ->
            initEdit(editText,object : EditTextListener{
                override fun onTextChangeListener(s: String) {
                    if (index != mEtNewCarrierViews.size-1){
                        mEtNewCarrierViews[index+1].requestFocus()
                    }
                }
            })

        }


    }

    var index = 0
    override fun initData() {
        viewModel.defUI.callObserve.observe(this) {
            when (it.type) {
                200 -> {
                    val entity = it.item as LotEntity
                    mLayoutViews[index].visibility = View.VISIBLE
                    mTvLotViews[index].text = entity.LOT.LOTID
                    mTvOldCarrierViews[index].text = entity.LOT.DURABLEID
                    index++
                    etCarrierID.setText("")
                    etLot.setText("")
                }
                100 ->{
                    if (it.item.toString().isEmpty()){
                        viewModel.defUI.toastEvent.value = ToastMessageBean("载具更换成功",true)
                        finish()
                    }else{
                        viewModel.defUI.toastEvent.value = ToastMessageBean(it.item.toString() + "更换失败",false)
                    }
                }
            }
        }
    }
}